package com.koron.ticket.work.logicticket.ycpowerlinefirst;

import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.print.ReportTask;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.utils.PrintUtils;
import com.koron.ticket.work.common.bean.FifteenTables;
import com.koron.ticket.work.enums.PrintTemplate;
import com.koron.ticket.work.logicticket.hightask.bean.YcHightaskTicketPrint;
import com.koron.ticket.work.logicticket.ycpowerlinefirst.bean.SignDto;
import com.koron.ticket.work.logicticket.ycpowerlinefirst.bean.YcLineFirstDto;
import com.koron.ticket.work.workPermit.bean.WorkPermit;
import com.koron.ticket.work.workend.bean.WorkEnd;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("work/yclinefirst")
@Api(tags = "两票管理-工作票-线路一种")
public class YcLineFirstController implements WorkflowController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    private EamUser getCurrUser() {
        return ThreadLocalContext.get();
    }

    @Autowired
    private YcLineFirstService yclinefirstservice;


    /**
     * 填写阶段，保存功能
     * 1. 保存当前的数据（新增/更新）
     *
     * @param dto
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see FifteenTables
     */
    @PostMapping("/writeSave")
    @ApiOperation("填写-保存")
    public Response writeSave(@RequestBody YcLineFirstDto dto) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "writeSave", Response.class, dto);
    }

    /**
     * 打印预览功能：
     * 1. 所有状态的打印预览都是这个接口
     * 2. 接口返回的是一个url，用于拉起打印控件的打印预览
     * 3. 需要先安装打印控件，并确保控件正常运行
     *
     * @param id
     * @return Response对象
     * @version 1.0
     * @see Response
     */
    @GetMapping("/printById")
    @ApiOperation("打印预览")
    public Response print(@RequestParam("id") String id) {
        List<YcHightaskTicketPrint> printList = ADOConnection.runTask(getCurrDs(), yclinefirstservice, "getPrintData", ArrayList.class, id);
        String printTemplateId = PrintUtils.getDefaultPrintTemplateId(PrintTemplate.YcPowerLineFirst.getType());
        String url = ReportTask.printData(getCurrUser(), printTemplateId, printList);//存储打印数据，以及返回打印地址
        return Response.success("获取成功，拿返回地址回调打印", url);
    }

    @PostMapping("/signSave")
    @ApiOperation("待签发-保存")
    public Response signSave(@RequestBody SignDto dto) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "signSave", Response.class, dto);
    }

//    @PostMapping("/countersignSave")
//    @ApiOperation("待会签-保存")
//    public Response countersignSave(@RequestBody WorkFlow workFlow) {
//        return ADOConnection.runTask(getCurrDs(), hoistingservice, "countersignSave", Response.class, workFlow);
//    }

//    @PostMapping("/acceptSave")
//    @ApiOperation("待接收-保存")
//    public Response acceptSave(@RequestBody WorkPermit dto) {
//        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "permitSave", Response.class, dto);
//    }

    @PostMapping("/permitSave")
    @ApiOperation("待许可-保存")
    public Response permitSave(@RequestBody WorkPermit dto) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "permitSave", Response.class, dto);
    }

//    @PostMapping("/workEndSave")
//    @ApiOperation("工作待终结-保存")
//    public Response workEndSave(@RequestBody WorkEnd workEnd) {
//        return ADOConnection.runTask(getCurrDs(), tempuserpowerservice, "ticketEndSave", Response.class, workEnd);
//    }

    /**
     * 工作票待终结阶段，保存功能
     * 1. 保存工作票待终结阶段的数据（新增/更新）
     * 2. 保存签名的数据（新增/更新）
     *
     * @param workEnd
     * @return Response对象
     * @version 1.0
     * @see Response
     * @see WorkEnd
     */
    @PostMapping("/ticketEndSave")
    @ApiOperation("工作票待终结-保存")
    public Response ticketEndSave(@RequestBody WorkEnd workEnd) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "ticketEndSave", Response.class, workEnd);
    }

    /**
     * 工作流回调接口
     */
    @Override
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "workflowUpdate", Response.class, redirectQuery);
    }

    /**
     * 工作流结束接口：
     */
    @Override
    public Response updateEnd(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "workflowUpdateEnd", Response.class, redirectQuery);
    }

    /**
     * 工作流作废：
     */
    @Override
    public Response updateCancel(@RequestBody RedirectQuery redirectQuery) {
        return ADOConnection.runTask(getCurrDs(), yclinefirstservice, "workflowUpdateCancel", Response.class, redirectQuery);
    }
}
